package com.wwwarehouse.contract.orders.import_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.bean.ImporderMonitorBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;


/**
 * 订单获取监控Fragment
 * Created by ShineF on 2017/9/20 0020.
 */
@Route(path = "/contract/ImportOrdersMonitorPagerFragment")
public class ImportOrdersMonitorPagerFragment extends BaseFragment implements ElasticScrollView.OnPullListener, CustomViewPagerInternal.OnPageChangeListener {

    private StateLayout mStateLayout = null;
    private CustomViewPagerInternal mCustomPager = null;
    private CirclePageIndicator mCirclePager = null;
    private List<Fragment> mList;
    private ElasticScrollView mCustomView = null;
    private boolean isShow = false;
    private ClearEditText mSearchEdt = null;
    private TextView mTvSearchView = null;
    private LinearLayout mCardItem = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_import_orders_monitor_pager, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mStateLayout = $(R.id.custom_state_layout);
        mCustomPager = $(R.id.vp_custom);
        mCirclePager = $(R.id.pager_indicator);
        mCustomView = $(R.id.esv_view);
        mTvSearchView = $(R.id.search_no_data);
        mCardItem = $(R.id.ll_content);
        mTvSearchView.setVisibility(View.GONE);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ImportOrdersMonitorPagerFragment) {
            mActivity.setTitle(R.string.select_shop);
        }
    }

    @Override
    public void requestDatas() {
        ((BaseCardDeskActivity) mActivity).hideSortBt();
        ((BaseCardDeskActivity) mActivity).hideFilterBt();
        mStateLayout.showProgressView(true);
        mCustomView.setOnPullListener(this);
        mList = new ArrayList<>();
        mStateLayout.setSystemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestHttp("");
            }
        });
        requestHttp("");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        isShow = !isShow;
    }

    public void requestHttp(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "1");
        map.put("itemType", "H_SHOP");
        map.put("name", name);
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("page", "1");
        queryMap.put("size", "9");
        map.put("query", queryMap);
        loadData(ContractConstant.GET_IMPORT_TOOLS_MONITOR_INDEX, map, 1);
    }

    public void loadData(String url, Map params, int what) {
        NoHttpUtils.httpPost(url, params, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if (commonClass.getCode().equals("0")) {
                    if (null != commonClass.getData()) {
                        ImporderMonitorBean data = JSON.parseObject(commonClass.getData().toString(), ImporderMonitorBean.class);
                        if (data.getList() != null && data.getList().size() != 0 && mSearchEdt.getVisibility() == View.GONE) {
                            mStateLayout.showEmptyView("数据空空如也", true);
                        } else {
                            setData(data);
                        }
                    }
                } else {
                    mStateLayout.showSystemView(commonClass.getMsg(), false);
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {
                mStateLayout.showContentView();
            }
        }, what);
    }

    public void setData(ImporderMonitorBean data) {
        mList.clear();
        if (null != data) {
            ArrayList<ImporderMonitorBean.ListBean> dataList = data.getList();
            int total = data.getTotal();
            int page;
            if (total % 9 == 0) {
                page = total / 9;
            } else {
                page = (int) (total / 9f) + 1;
            }
            for (int i = 0; i < page; i++) {
                ImportOrdersMonitorPagerContentFragment fragment = new ImportOrdersMonitorPagerContentFragment();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("data", dataList);
                bundle.putInt("page", i + 1);
                fragment.setArguments(bundle);
                mList.add(fragment);
            }
            if (isAdded()) {
                mCustomPager.setOffscreenPageLimit(5);
                MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mList);
                mCustomPager.setAdapter(mAdapter);
                mCirclePager.setViewPager(mCustomPager);
                mCirclePager.setOnPageChangeListener(this);
                if (dataList.size() == 0) {
                    mTvSearchView.setVisibility(View.VISIBLE);
                    mCardItem.setVisibility(View.GONE);
                } else {
                    mTvSearchView.setVisibility(View.GONE);
                    mCardItem.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void searchOnRealTimeClick(String text) {
        super.searchOnRealTimeClick(text);
        String searchRuleName;
        if (TextUtils.isEmpty(text)) {
            searchRuleName = "";
        } else {
            searchRuleName = text;
        }
        requestHttp(searchRuleName);
    }

    @Override
    public void onDownPull() {
        if (!isShow) {
            ((BaseCardDeskActivity) mActivity).showSearchTitle();
        } else {
            ((BaseCardDeskActivity) mActivity).hideSearchTitle();
            ((BaseCardDeskActivity) mActivity).hideSearchInput();
            hideSoftKeyBoard(mSearchEdt);
        }
        isShow = !isShow;
    }

    @Override
    public void onUpPull() {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        mSearchEdt.clearFocus();
        hideSoftKeyBoard(mSearchEdt);
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
